package net.wantongtech.xa.uploadbasicinfo.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wtkj.constant.NewClearingConstant;
import com.wtkj.vo.RandomGUID;
import com.wtkj.vo.dzzf.base.xxjh.MessageSend;
import com.wtkj.zwxt.clearing.vo.push.DetailVo;
import com.wtkj.zwxt.clearing.vo.push.HoldWithTransVo;
import com.wtkj.zwxt.clearing.vo.push.TransactionVo;
import lombok.extern.slf4j.Slf4j;
import net.wantongtech.xa.uploadbasicinfo.dao.CommonBaseDao;
import net.wantongtech.xa.uploadbasicinfo.dao.CreateCMBCTransPackageDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.wtkj.constant.NewClearingConstant.PublishType.SELF;
import static com.wtkj.zwxt.enums.SignSourceEnum.SELF_PUBLISH;

/**
 * 创建民生银行交易包
 */
@Slf4j
@Service
public class CreateCMBCTransPackageService {


    @Resource
    private CreateCMBCTransPackageDao createCMBCTransPackageDao;

    private ObjectMapper objectMapper = new ObjectMapper();

    @Resource
    private CommonBaseDao commonBaseDao;

    public void create() {
        try {
            List<TransactionVo> transactionVoList = createCMBCTransPackageDao.querySendingTransaction();
            log.info("共查询到{}交易信息", transactionVoList.size());

            transactionVoList.forEach(transactionVo -> {
                DetailVo detailVo = new DetailVo();
                detailVo.setTransactionId(transactionVo.getIntegratedId());
                detailVo.setFee(transactionVo.getFee());
                transactionVo.getDetails().add(detailVo);
                transactionVo.setDescription("手动补发交易，消费0元");
                transactionVo.setIsIntegrated(NewClearingConstant.IntegratedState.NONE);
                transactionVo.setCardId("61071542230150000003");
            });

            int totalCnt = transactionVoList.size();
            Long totalFee = transactionVoList.stream()
                    .map(TransactionVo::getFee)
                    .reduce(Long::sum)
                    .orElse(0L);
            HoldWithTransVo holdWithTransVo = new HoldWithTransVo();
            String channelType = "CMBC";
            holdWithTransVo.setMessageId(7275L);
            holdWithTransVo.setCount(totalCnt);
            holdWithTransVo.setAmount(totalFee);
            holdWithTransVo.setPublishType(SELF);
            holdWithTransVo.setTransaction(transactionVoList);

            saveMessageSend(holdWithTransVo, channelType);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    private long generateMessageId(String channelType) {
        return commonBaseDao.querySeqNetVal(String.format("SEQUENCE%s", channelType));
    }

    private void saveMessageSend(HoldWithTransVo holdWithTransVo,
                                 String channelType) throws JsonProcessingException {
        MessageSend messageSend = new MessageSend();
        messageSend.setListId(new RandomGUID().ToRawString());
        messageSend.setCount(holdWithTransVo.getCount());
        messageSend.setAmount(holdWithTransVo.getAmount());
        messageSend.setChannelType(channelType);
        messageSend.setMsgId(holdWithTransVo.getMessageId());
        messageSend.setCreateOn(new Date());
        messageSend.setMsgBody(objectMapper.writeValueAsString(holdWithTransVo));
        messageSend.setSignSource(SELF_PUBLISH.getSource());
        createCMBCTransPackageDao.createValueObject(messageSend);
    }
}
